package com.liuwei.desktop.app;

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.liuwei.desktop.common.FileUtils;
import com.liuwei.desktop.modal.entity.TUser;
import com.liuwei.desktop.modal.repository.UserService;
import com.liuwei.desktop.modal.repository.impl.UserServiceImpl;
import com.liuwei.desktop.view.SearchView;

/**
 * Hello world!
 *
 */
public class AppLauncher {
	
    public static void main( String[] args ){
    	System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
    	try {
    		//应用程序初始化
    		APPInitializer.launch();

            // 显示应用 GUI
            //createAndShowGUI();
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					UncaughtExceptionHandler eh = new GlobalExceptionHandler();
    					Thread.setDefaultUncaughtExceptionHandler(eh);
    					System.out.println(Thread.currentThread().getName());
    					SearchView window = new SearchView();
    					window.getFrame().setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    		
    		/*UserService userService = new UserServiceImpl();
    		String path = "e://application.txt";
    		List<String> names = FileUtils.getRows(path);
    		if(names!=null && names.size()>0){
    			List<TUser> users = new ArrayList<>();
    			TUser user = null;
    			for(int i=0;i<names.size();i++){
    				System.out.println(i+"/"+names.size()+" = "+names.get(i));
    				user = new TUser();
    				String name = names.get(i);
    				name = name.replace("'", "");
    				user.setName(name);
    				user.setTel("应用宝");
    				users.add(user);
    				//userService.insertUser(user);
    			}
    			userService.batchInsertUser(users);
    		}*/
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 创建并显示GUI。出于线程安全的考虑，
     * 这个方法在事件调用线程中调用。
     * @throws Exception 
     */
    private static void createAndShowGUI() throws Exception {
        // 创建及设置窗口
    	String AppName = APPInitializer.getAppProperties().get("app.name");
        JFrame frame = new JFrame(AppName);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        // 添加 "Hello World" 标签
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);
        frame.getContentPane().setLayout(new GridLayout());
        
        UserService service = new UserServiceImpl();
        List<TUser> users = service.getAllUser();
        if(users!=null&&users.size()>0) {
        	for(int i=0;i<users.size();i++){
        		String message = users.get(i).getPkid()+":"+users.get(i).getName();
        		System.out.println(message);
                JLabel l = new JLabel(message);
                frame.getContentPane().add(l);
        	}
        }

        // 显示窗口
        frame.setVisible(true);
    }
}
